package com.example.baseweb.service.impl;

import com.example.baseweb.entity.Category;
import com.example.baseweb.repository.CategoryRepository;
import com.example.baseweb.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    
    @Autowired
    private CategoryRepository categoryRepository;
    
    @Override
    public List<Category> getAllCategories() {
        return categoryRepository.findAllOrderBySortOrder();
    }
    
    @Override
    public Category getCategoryById(Long id) {
        return categoryRepository.findById(id).orElse(null);
    }
    
    @Override
    public Category saveCategory(Category category) {
        if (category.getId() == null) {
            category.setCreateTime(LocalDateTime.now());
        }
        category.setUpdateTime(LocalDateTime.now());
        return categoryRepository.save(category);
    }
    
    @Override
    public void deleteCategory(Long id) {
        categoryRepository.deleteById(id);
    }
    
    @Override
    public Category updateCategoryStatus(Long id, Integer status) {
        Category category = getCategoryById(id);
        if (category != null) {
            category.setStatus(status);
            category.setUpdateTime(LocalDateTime.now());
            return categoryRepository.save(category);
        }
        return null;
    }
    
    @Override
    public List<Category> getCategoriesByStatus(Integer status) {
        return categoryRepository.findByStatus(status);
    }
} 